using IdentityServer4.AccessTokenValidation;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Ocelot.DependencyInjection;
using Ocelot.Middleware;
using Ocelot.Provider.Consul;
using Service.Framework;
using Service.Framework.ConsulExtends;

var builder = WebApplication.CreateBuilder(args);

builder.Configuration.AddJsonFile("ocelot.json", optional: false, reloadOnChange: true);
// 鉴权设置
var authenticationProviderKey = "ServiceA";
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
    .AddIdentityServerAuthentication(authenticationProviderKey, options =>
    {
        options.Authority = "http://localhost:5141"; // id4服务地址
        options.ApiName = "ServiceA"; // id4 api资源里的apiname
        options.RequireHttpsMetadata = false; //不使用https
        options.SupportedTokens = SupportedTokens.Both;
    });
authenticationProviderKey = "ServiceB";
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
    .AddIdentityServerAuthentication(authenticationProviderKey, options =>
    {
        options.Authority = "http://localhost:5141"; // id4服务地址
        options.ApiName = "ServiceB"; // id4 api资源里的apiname
        options.RequireHttpsMetadata = false; //不使用https
        options.SupportedTokens = SupportedTokens.Both;
    });

// 添加consul 网关信息
builder.Services.AddOcelot().AddConsul();

builder.Services.AddLogging(builder =>
{
    builder.AddFilter("/ocelot", LogLevel.Warning);
    builder.AddLog4Net();
});

var app = builder.Build();

app.UseOcelot();

app.Run();